﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using Microsoft.Web.Helpers;
using VideoSeller.Business.Models;
using VideoSeller.Web.Models;

namespace VideoSeller.Web.Controllers
{
	[AllowAnonymous]
	public class AccountController : BaseController
	{
		private readonly ClientModel _clientModel;
		private readonly MembershipUserModel _membershipUserModel;

		public AccountController(ClientModel clientModel, MembershipUserModel membershipUserModel)
		{
			_clientModel = clientModel;
			_membershipUserModel = membershipUserModel;
		}

		[HttpGet]
		public ActionResult Login()
		{
			var model = new LoginViewModel();
			return View(model);
		}

		[HttpPost]
		public ActionResult Login(LoginViewModel model)
		{
			if (ModelState.IsValid)
			{
				if (Membership.ValidateUser(model.Login, model.Password))
				{
					FormsAuthentication.SetAuthCookie(model.Login, true);
					return RedirectToAction("Index", "Home");
				}
                ModelState.AddModelError("InvalidLogin", HttpContext.GetGlobalResourceObject("Common", "ValidLoginPassword").ToString());
			}
			return View(model);
		}

		[HttpGet]
		public ActionResult Logout()
		{
			FormsAuthentication.SignOut();

			return RedirectToAction("Index", "Home");
		}

        [HttpGet]
        public ViewResult Register(String id)
        {
            var model = new RegisterViewModel();
            model.FromUserGuid = id;
            return View(model);
        }
		[HttpPost]
		public ActionResult Register(RegisterViewModel model)
		{
           
			if (Membership.GetUser(model.Login) != null)
			{
				ModelState.AddModelError("InvalidLogin", "Login already exists");
			}
			if (Membership.FindUsersByEmail(model.Email).Count > 0)
			{
				ModelState.AddModelError("InvalidEmail", "Email already exists");
			}

            //DEBUG - Hapaniuk close
            //if (!ReCaptcha.Validate())
            //{
            //    ModelState.AddModelError("ReCaptcha", "Captcha is invalid");
            //}

			if (ModelState.IsValid)
			{
				if (_clientModel.RegisterClient(model.Login, model.Password, model.Email, model.FromUserGuid))
				{
					model.IsRegistered = true;
					return View(model);
				}
				ModelState.AddModelError("InvalidRegistration", "Registration failed");
			}

			return View(model);
		}

		[HttpGet]
		public ActionResult Confirm(Guid u, Guid a)
		{
			_clientModel.ApproveClient(u, a);

			return RedirectToAction("Index", "Home");
		}

		[HttpGet]
		public ActionResult ForgotPassword()
		{
			var model = new ForgotPasswordViewModel();
			return View(model);
		}

		[HttpPost]
		public ActionResult ForgotPassword(ForgotPasswordViewModel model)
		{
			if (ModelState.IsValid)
			{
				var newPassword = _membershipUserModel.ResetPassword(model.Email);

				if (!string.IsNullOrEmpty(newPassword))
				{
					model.IsPasswordSend = true;
					return View(model);
				}
			}

            ModelState.AddModelError("InvalidEmail", HttpContext.GetGlobalResourceObject("Common", "UserNotExist").ToString());

			return View(model);
		}

		[HttpDelete]
		public ActionResult Remove(Guid id)
		{
			_clientModel.DeleteClient(id, true);
			return RedirectToAction("Index", "Home");
		}
	}
}